package DBLayer;
import ModelLayer.DiscountType;
import java.sql.*;
import java.util.ArrayList;

public class DBDiscountType {
    // <editor-fold defaultstate="collapsed" desc="Attributes & Constructors">
    private Connection conn;

    public DBDiscountType() {
        conn = SQLConnector.getInstance().getConnection();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL SELECT Methods">
    public DiscountType getDiscountTypeByID(int id) {
        return selectSingle("SELECT `id_discount_type`, `name` FROM `ps_discount_type` LEFT JOIN `ps_discount_type_lang` USING (`id_discount_type`) WHERE (`id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL) AND `id_discount_type` = " + id + " LIMIT 0, 1;");
    }

    public ArrayList<DiscountType> getDiscountTypeList() {
        return selectMultiple("SELECT `id_discount_type`, `name` FROM `ps_discount_type` LEFT JOIN `ps_discount_type_lang` USING (`id_discount_type`) WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " OR `id_lang` IS NULL ORDER BY `id_discount_type` ASC;");
    }

    private DiscountType selectSingle(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        DiscountType discountType = new DiscountType();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            if (result.next()) {
                discountType = buildDiscountType(result);
            }
            System.out.println("Fetching DiscountType Object");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return discountType;
    }

    private ArrayList<DiscountType> selectMultiple(String sqlStatement) {
        Statement stmt = null;
        ResultSet result = null;
        ArrayList<DiscountType> discountTypes = new ArrayList<DiscountType>();

        try {
            stmt = conn.createStatement();
            result = stmt.executeQuery(sqlStatement);

            while (result.next()) {
                DiscountType discountType = new DiscountType();
                discountType = buildDiscountType(result);
                discountTypes.add(discountType);
            }
            System.out.println("Fetching DiscountType List");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return discountTypes;
    }

    private DiscountType buildDiscountType(ResultSet result) {
        DiscountType discountType = new DiscountType();
        try {
            discountType.setIDDiscountType(result.getInt("id_discount_type"));
            discountType.setName(result.getString("name"));

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
        return discountType;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="SQL INSERT, UPDATE & DELETE Methods">
    public int addDiscountType(DiscountType discountType) {
        int returnID = 0;
        Statement stmt = null;
        ResultSet result = null;

        String query = "INSERT INTO `ps_discount_type_lang` (`id_discount_type`, `id_lang`, `name`) "
                + "VALUES (" + discountType.getIDDiscountType() + ", " + Constants.DEFAULT_LANGUAGE + ", '" + discountType.getName() + "');";

        query += "INSERT INTO `ps_discount_type` (`id_discount_type`) "
                + "VALUES (" + discountType.getIDDiscountType() + ");";
        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            result = stmt.getGeneratedKeys();
            if (result.next()) {
                returnID = result.getInt(1);
            }
            System.out.println("Added DiscountType ID " + returnID);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                result = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public int updateDiscountType(DiscountType discountType, int id) {
        int returnID = 0;
        Statement stmt = null;
	String query = "UPDATE `ps_discount_type` SET "
                + "`id_discount_type`   = "  + discountType.getIDDiscountType() + ", "
                + "WHERE `id_discount_type` = " + id + " LIMIT 1; ";

        query += "UPDATE `ps_discount_type_lang` SET "
                + "`id_discount_type`   = "  + discountType.getIDDiscountType() + ", "
                + "`name`               = "  + discountType.getName() + " "
                + "WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_discount_type` = " + id + " LIMIT 1;";

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate(query);
            returnID = discountType.getIDDiscountType();
            System.out.println("Updated DiscountType ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
        return returnID;
    }

    public void deleteDiscountType(int id) {
        Statement stmt = null;

        try {
            stmt = conn.createStatement();
            stmt.executeUpdate("DELETE FROM `ps_discount_type` WHERE `id_discount` = " + id + " LIMIT 1; DELETE FROM `ps_discount_type_lang` WHERE `id_lang` = " + Constants.DEFAULT_LANGUAGE + " AND `id_discount_type` = " + id + " LIMIT 1;");
            System.out.println("Deleted DiscountType ID " + id);
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    System.out.println("SQLException: " + e.getMessage());
                    System.out.println("SQLState: " + e.getSQLState());
                    System.out.println("VendorError: " + e.getErrorCode());
                }
                stmt = null;
            }
        }
    }
    // </editor-fold>
}
